package com.gams.device.led;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;

import java.awt.Color;

@Log4j
public class Protocol {

    // ====引入动作方式列表(数值从0开始)====
    // 0 = '随机',
    // 1 = '立即显示',
    // 2 = '左滚显示',
    // 3 = '上滚显示',
    // 4 = '右滚显示',
    // 5 = '下滚显示',
    // 6 = '连续左滚显示',
    // 7 = '连续上滚显示',
    // 8 = '连续右滚',
    // 9 = '连续下滚',
    // 10 = '中间向上下展开',
    // 11 = '中间向两边展开',
    // 12 = '中间向四周展开',
    // 13 = '从右向左移入',
    // 14 = '从左向右移入',
    // 15 = '从左向右展开',
    // 16 = '从右向左展开',
    // 17 = '从右上角移入',
    // 18 = '从右下角移入',
    // 19 = '从左上角移入',
    // 20 = '从左下角移入',
    // 21 = '从上向下移入',
    // 22 = '从下向上移入',
    // 23 = '横向百叶窗',
    // 24 = '纵向百叶窗',
    // =====================================

    // ====引出动作方式列表(数值从0开始)====
    // 0 = '随机',
    // 1 = '不消失',
    // 2 = '立即消失',
    // 3 = '上下向中间合拢',
    // 4 = '两边向中间合拢',
    // 5 = '四周向中间合拢',
    // 6 = '从左向右移出',
    // 7 = '从右向左移出',
    // 8 = '从右向左合拢',
    // 9 = '从左向右合拢',
    // 10 = '从右上角移出',
    // 11 = '从右下角移出',
    // 12 = '从左上角移出',
    // 13 = '从左下角移出',
    // 14 = '从下向上移出',
    // 15 = '从上向下移出',
    // 16 = '横向百叶窗',
    // 17 = '纵向百叶窗'
    // =====================================

    // ====停留动作方式列表(数值从0开始)====
    // 0 = '静态显示',
    // 1 = '闪烁显示'
    // =====================================

    public static MyUdpSocket my_udp = new MyUdpSocket(8811);
    public static LEDSender2010 ledsender = new LEDSender2010();
    //public static String ledhost = "127.0.0.1";
    public static String ledhost = "192.168.16.254";
    public static int ledport = 6666;
    public static int addr = 0;

    public static void mysleep(int msec) {
        try {
            Thread.sleep(msec);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //发送UDP包，buffer为发送数据，size为发送数据长度
    public static void udp_send(byte[] buffer, int size, String host, int port) {
        byte[] packet = new byte[size];
        LEDSender2010.blockCopy(packet, 0, buffer, 0, size);
        LEDSender2010.print_stream(packet, size);
        try {
            my_udp.send(host, port, packet, size);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //接收UDP包，buffer为接收数据，size为接收数据长度
    public static int udp_receive(byte[] buffer) {
        int size;
        size = my_udp.receive();
        if (size > 0) {
            my_udp.get_receive_packet(buffer, size);
        }
        return size;
    }

    public static int do_command(byte[] buffer, int size, String host, int port) {
        byte[] packet = new byte[1280];
        int tx, tx_repeat = 5;
        int isize;
        int i;

        for (tx = 0; tx < tx_repeat; tx++) {
            udp_send(buffer, size, host, port);
            for (i = 0; i < 50; i++) {
                isize = udp_receive(packet);
                if (isize > 0 && ledsender.parse_cmd_respond(packet, isize) == 1) {
                    return 1;
                }
            }
        }
        log.info("超时");
        return 0;
    }

    public static void demo_reset_display() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_reset_display(packet, (byte) addr, 1);
        log.info("复位节目显示...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_power_on() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_set_power(packet, (byte) addr, 1);
        log.info("打开电源...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_power_off() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_set_power(packet, (byte) addr, 0);
        log.info("关闭电源...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_get_power() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_get_power(packet, (byte) addr);
        log.info("读取电源状态...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_set_bright() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_set_bright(packet, (byte) addr, 7);
        log.info("设置亮度...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_get_bright() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_get_bright(packet, (byte) addr);
        log.info("读取亮度...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_adjust_time() {
        int size;
        byte[] packet = new byte[1280];
        //Calendar calendar = Calendar.getInstance();
        //指定时间校时，12小时制
        //size=ledsender.pkg_adjust_time_ex(packet, (byte)addr, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_WEEK)-1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
        //指定时间校时，24小时制
        //size=ledsender.pkg_adjust_time_ex(packet, (byte)addr, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_WEEK)-1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
        //以当前系统时间校时
        size = ledsender.pkg_adjust_time(packet, (byte) addr);
        log.info("校正时间...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_switch_on() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_set_switch(packet, (byte) addr, 1);
        log.info("开启5V输出...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_switch_off() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_set_switch(packet, (byte) addr, 0);
        log.info("关闭5V输出...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_get_boardparam() {
        int size;
        byte[] packet = new byte[1280];
        size = ledsender.pkg_get_boardparam(packet, (byte) addr);
        log.info("读取控制卡参数...");
        do_command(packet, size, ledhost, ledport);
    }

    public static void demo_set_boardparam_ip(String ip) {
        int size;
        byte[] packet = new byte[1280];
        byte[] ips = new byte[4];
        int i, k, k1, x;
        size = ledsender.pkg_get_boardparam(packet, (byte) addr);
        log.info("读取控制卡参数...");
        if (do_command(packet, size, ledhost, ledport) == 1) {
            k = 0;
            x = 0;
            ip = ip + '.';
            for (i = 0; i < 4; i++) {
                k1 = ip.indexOf('.', k);
                if (k1 > k) {
                    ips[i] = (byte) Integer.parseInt(ip.substring(k, k1));
                    k = k1 + 1;
                    x++;
                }
            }
            if (x == 4) {
                size = ledsender.pkg_set_boardparam_ip(packet, (byte) addr, ips);
                if (size > 0) {
                    do_command(packet, size, ledhost, ledport);
                }
            }
        }
    }

    public static void demo_power_schedule() {
        int size;
        byte[] packet = new byte[1280];
        //按一周7天每日定时开关屏，每日可以定义3个时段
        size = ledsender.pkg_power_schedule_weekday(packet, (byte) addr, true);
        //按指定的起止日期时间播放，一共可以定义21个时段
        //size=ledsender.pkg_power_schedule_period(packet, (byte)addr, true);
        log.info("设置定时开关屏计划...");
        do_command(packet, size, ledhost, ledport);
    }

    //dib_buffer为点阵图片的RGB数组，数组为整形数组，数组的每个单元代表一个像素点的RGB值
    //  像素点(X,Y)的RGB值按照一列一列顺序排列，例如宽64，高32的屏
    //    (0,0),(0,1),(0,2),...(0,31)  (1,0),(1,1),(1,2),...(1,31)  (2,0),(2,1),(2,2),...(2,31)  ... (63,0),(63,1),(63,2),...(63,31)
    //    像素点的RGB值用整数表示，0-7位表示红色，8-15位表示绿色，16-23表示蓝色，24-31位保留置0
    //    即：dib_buffer[n]&0xFF 表示红色
    //        (dib_buffer[n]>>8)&0xFF 表示绿色
    //        (dib_buffer[n]>>16)&0xFF 表示蓝色
    public static int dib_width = 64;
    public static int dib_height = 64;
    public static int dib_buffer_size = dib_width * dib_height;
    public static int[] dib_buffer = new int[dib_buffer_size];

    public static void draw_dib() {
        int x, y, z = 0;
        int seek = 0;
        int color = 0xFF;
        for (x = 0; x < dib_width; x++) {
            for (y = 0; y < dib_height; y++) {
                dib_buffer[seek++] = color;
            }
            z++;
            if (z >= 4) {
                switch (color) {
                    case 0xFF:
                        color = 0xFF00;
                        break;
                    case 0xFF00:
                        color = 0xFFFF;
                        break;
                    case 0xFFFF:
                        color = 0xFF;
                        break;
                    default:
                        color = 0xFF;
                        break;
                }
                z = 0;
            }
        }
    }

    public static void send_data(String host, int port) {
        int size = 0;
        int rsize;
        byte[] packet = new byte[1280];
        int i, j, k, x, r;
        int tx;
        int tx_repeat = 5;
        int recv_times = 20;
        boolean ok;
        //下面为通讯代码，包括数据的拆分打包、发送
        //    由于是UDP通讯，增加

        //如果数据量大于(1<<21为2M)
        if (ledsender.GetDataSize() > (1 << 22)) {
            log.info("LED节目数据长度超出显示屏存储空间");
            return;
        }

        log.info("LED节目压缩前数据长度" + ledsender.GetDataSize() + "，数据包数量" + ledsender.get_pkg_count(512));
        ledsender.Compress();
        log.info("LED节目压缩后数据长度" + ledsender.GetDataSize() + "，数据包数量" + ledsender.get_pkg_count(512));

        //起始包
        ok = false;
        log.info("发送起始包...，序列号=0");
        for (tx = 0; tx < tx_repeat; tx++) {
            size = ledsender.pkg_begin(packet, addr);
            udp_send(packet, size, host, port);
            for (x = 0; x < recv_times; x++) {
                rsize = udp_receive(packet);
                if (rsize > 0 && ledsender.parse_trans_respond(packet, rsize, LEDSender2010.PKC_BEGIN, 0) > 0) {
                    log.info("起始包发送完成，序列号=0");
                    ok = true;
                    break;
                }
            }
            if (ok) break;
        }
        if (!ok) {
            log.info("超时");
            return;
        }

        //数据包和结束包
        k = ledsender.get_pkg_count(512);
        i = 1;
        while (i <= k + 1) {
            ok = false;
            if (i <= k) {
                log.info("发送数据包...，序列号=" + i);
                size = ledsender.pkg_data(packet, addr, i, 512);
            } else {
                log.info("发送结束包...，序列号=" + i);
                size = ledsender.pkg_end(packet, addr, i);
            }
            for (tx = 0; tx < tx_repeat; tx++) {
                j = i;
                udp_send(packet, size, host, port);
                for (x = 0; x < recv_times; x++) {
                    rsize = udp_receive(packet);
                    if (rsize > 0) {
                        if (i <= k) {
                            r = ledsender.parse_trans_respond(packet, rsize, LEDSender2010.PKC_DATA, i);
                        } else {
                            r = ledsender.parse_trans_respond(packet, rsize, LEDSender2010.PKC_END, i);
                        }
                        switch (r) {
                            case 0x1:
                                if (i <= k) {
                                    log.info("数据包发送完成，序列号=" + i);
                                } else {
                                    log.info("结束包发送完成，序列号=" + i);
                                }
                                j = i + 1;
                                ok = true;
                                break;
                            case 0x2:
                                if (i <= k) {
                                    log.info("数据包序列号校对错误，应收=" + i + "，实收=" + ledsender.fix_serialno);
                                } else {
                                    log.info("结束包序列号校对错误，应收=" + i + "，实收=" + ledsender.fix_serialno);
                                }
                                j = ledsender.fix_serialno;
                                ok = true;
                                break;
                        }
                        //ok=true;
                        //break;
                    }
                    if (ok) break;
                }
                i = j;
                if (ok) break;
            }
            if (!ok) {
                log.info("超时");
                return;
            }
        }

        /*
        ok=false;
        for (x=0; x<100; x++){
        	size=udp_receive(packet);
        	switch (ledsender.parse_notify(packet, size)){
        	case LEDSender2010.NOTIFY_ROOT_PLAY:
                log.info("发送节目数据校验成功");
                ok=true;
                break;
        	case LEDSender2010.NOTIFY_ROOT_DOWNLOAD:
                log.info("下载节目数据校验成功");
        		ok=true;
                break;
        	}
        	if (ok) break;
        }
        if (!ok) log.info("节目数据校验错误，发送/下载失败");
        */
    }

    /*
    public static void send_data(String host, int port){
    	int size=0;
        byte[] packet = new byte[1280];
        int i, j, k, x, r;
        int tx, tx_repeat=5;
        boolean ok;
        //下面为通讯代码，包括数据的拆分打包、发送
        //    由于是UDP通讯，增加

        //如果数据量大于(1<<21为2M)
        if (ledsender.GetDataSize()>(1<<22)){
            log.info("LED节目数据长度超出显示屏存储空间");
            return;
        }

        log.info("LED节目压缩前数据长度"+ledsender.GetDataSize()+"，数据包数量"+ledsender.get_pkg_count(512));
        ledsender.Compress();
        log.info("LED节目压缩后数据长度"+ledsender.GetDataSize()+"，数据包数量"+ledsender.get_pkg_count(512));

        //起始包
        ok=false;
        log.info("发送起始包...，序列号=0");
        for (tx=0; tx<tx_repeat; tx++){
            size=ledsender.pkg_begin(packet, addr);
            udp_send(packet, size, host, port);
            for (x=0; x<50; x++){
            	size=udp_receive(packet);
            	if (size>0 && ledsender.parse_trans_respond(packet, size, LEDSender2010.PKC_BEGIN, 0)>0){
                    log.info("起始包发送完成，序列号=0");
                    ok=true;
                    break;
            	}
            }
            if (ok) break;
        }
        if (!ok){
            log.info("超时");
            return;
        }

        //数据包
        k=ledsender.get_pkg_count(512);
        i=1;
        while (i<=k){
       	    ok=false;
            log.info("发送数据包...，序列号="+i);
            for (tx=0; tx<tx_repeat; tx++){
            	j=i;
                size=ledsender.pkg_data(packet, addr, i, 512);
                udp_send(packet, size, host, port);
                for (x=0; x<50; x++){
                	size=udp_receive(packet);
                	if (size>0){
                		r=ledsender.parse_trans_respond(packet, size, LEDSender2010.PKC_DATA, i);
                		switch(r){
                		case 0x1:
                        	log.info("数据包发送完成，序列号="+i);
                            ok=true;
                            j=i+1;
                			break;
                		case 0x2:
                        	log.info("数据包序列号校对错误，应收="+i+"，实收="+ledsender.fix_serialno);
                			j=ledsender.fix_serialno;
                			break;
                		}
                        //ok=true;
                        //break;
                	}
                    if (ok) break;
                }
                i=j;
                if (ok) break;
            }
            if (!ok){
                log.info("超时");
                return;
            }
        }


        //结束包
        ok=false;
        log.info("发送结束包...，序列号="+i);
        for (tx=0; tx<tx_repeat; tx++){
            size=ledsender.pkg_end(packet, addr, i);
            udp_send(packet, size, host, port);
            for (x=0; x<50; x++){
            	size=udp_receive(packet);
            	if (size>0 && ledsender.parse_trans_respond(packet, size, LEDSender2010.PKC_END, i)>0){
                    log.info("结束包发送完成，序列号="+i);
                    ok=true;
                    break;
            	}
            }
            if (ok) break;
        }
        if (!ok){
            log.info("超时");
            return;
        }
    }
    */

    public static void demo_upgrade() {
        ledsender.Upgrade("d:\\vsd.bin");
        send_data(ledhost, ledport);
    }

    public static void demo_play_text(String context) {
        //生成节目数据
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
        ledsender.AddChapter(0, 1000);
        ledsender.AddRegion(0, 0, 128, 64);
        //点阵文字（单行，支持对齐方式）
        ledsender.AddLeaf(0, 2000);
        ledsender.AddTextEx(0, 0, 128, 64, context, LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 16, Color.BLACK, new Color(247, 243, 255), 0, 1, 0, 2, 0, 0, 0, 0);
        send_data(ledhost, ledport);
    }

    public static void demo_play() {
        //String[] formats={"#y", "年", "#m", "月", "#d", "日", " ", "#h", ":", "#n", ":", "#s"};
        //String[] formats={"#h", ":", "#n", ":", "#s"};

        //生成节目数据
        //ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
        //ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_FULL);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(0, 0, 256, 64);

        /*
        //点阵图片
        ledsender.AddLeaf(1, 2000);
        //draw_dib();
        //ledsender.AddDib(0, 0, 64, 64, dib_buffer, dib_buffer_size, dib_width, dib_height, 1, 5, 2, 5, 0, 0, 1000);
        BufferedImage bi;
        try {
            bi = LEDSender2010.createImageEx("中华人民共和国", new Font("宋体", Font.BOLD, 24), Color.BLACK, Color.CYAN, 0, false, 64);
            log.info("width:"+bi.getWidth()+"height:"+bi.getHeight());
            ledsender.AddWindow(0, 0, 64, 64, LEDSender2010.getBufferedImageRGB(bi), bi.getWidth() * bi.getHeight(), bi.getWidth(), bi.getHeight(), 2, 0, 2, 0, 0, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        */

        ledsender.AddLeaf(1, 2000);
        //点阵文字（单行，支持对齐方式）
        ledsender.AddTextEx(0, 0, 384, 32, "hello欢迎光临谢谢惠顾欢欢迎光临谢谢惠顾欢", LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 24, Color.BLACK, new Color(255, 255, 255), 0, 27, 0, 2, 0, 0, 0, 0);
        //点阵文字（多行例子）
        //ledsender.AddText(0, 32, 128, 32, "欢迎光临\r\n谢谢惠顾",  "宋体", 16, Color.BLACK, new Color(255,255,255), 0, 0, true, 3, 0, 2, 0, 0, 0, 0);

        //ledsender.AddLeaf(1, 2000);
        //ledsender.AddTextEx(0, 0, 128, 32, "同一个中国", LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 16, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        //ledsender.AddTextEx(128, 0, 160, 32, "同一个梦想", LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 16, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);

        /*
        //24点阵内码文字
        ledsender.AddLeaf(1, 2000);
        ledsender.AddString(0, 0, 64, 32, "欢迎光临abc", 2, 0, 2, 0, 0, 0, 0, 0xFF, 1);
        //24点阵日期时间，格式yyyy年mm月dd日
        ledsender.AddLeaf(1, 2000);
        ledsender.AddStringDateTime(0, 0, 256, 32, 0xFFFF, 1, formats, 6);
        */

        send_data(ledhost, ledport);
    }

    public static void demo_play2() {

        String[] formats_YMDHNS = {"#y", "年", "#m", "月", "#d", "日", " ", "#h", ":", "#n", ":", "#s"};
        String[] formats_WEEK = {"星期", "#w"};
        String[] formats_YMD = {"#y", "年", "#m", "月", "#d", "日"};
        String[] formats_HNS = {"#h", ":", "#n", ":", "#s"};

        //生成节目数据
        //ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(0, 0, 384, 64);
        ledsender.AddLeaf(0, 20000);
        ledsender.AddText(0, 8, 128, 16, "①社保业务", "宋体", 16, Color.BLACK, Color.YELLOW, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(64, 8, 128, 16, "②社保业务", "宋体", 16, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(128, 8, 128, 16, "③社保业务", "宋体", 16, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(192, 8, 128, 16, "④综合服务", "宋体", 16, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(256, 8, 128, 16, "⑤综合服务", "宋体", 16, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(320, 8, 128, 16, "⑥综合服务", "宋体", 16, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
//        ledsender.AddLeaf(1, 2000);
//        ledsender.AddDateTime(0, 0, 128, 16, formats_YMDHNS, 12, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddDateTime(0, 16, 128, 16, formats_WEEK, 2, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddLeaf(1, 2000);
//        ledsender.AddDateTime(0, 0, 128, 16, formats_YMD, 6, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddDateTime(0, 16, 128, 16, formats_HNS, 5, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddLeaf(1, 2000);
//        ledsender.AddText(0, 0, 128, 16, "安全生产(天数)", "宋体", 16, Color.BLACK, Color.YELLOW, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
//        ledsender.AddCounter(0, 16, 128, 16, 2020, 5, 1, 8, 0, 0, 0, 6, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddLeaf(1, 2000);
//        ledsender.AddText(0, 0, 128, 16, "距离国庆节(天)", "宋体", 16, Color.BLACK, Color.YELLOW, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
//        ledsender.AddCounter(0, 16, 128, 16, 2020, 10, 1, 0, 0, 0, 1, 6, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddLeaf(1, 2000);
//        ledsender.AddText(0, 0, 128, 16, "2分钟正计时", "宋体", 16, Color.BLACK, Color.YELLOW, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
//        ledsender.AddCounter(0, 16, 128, 16, 0, 0, 0, 0, 2, 0, 2, 0, "宋体", 16, Color.BLACK, Color.YELLOW, 0);
//        ledsender.AddLeaf(1, 2000);
//        ledsender.AddText(0, 0, 128, 16, "2分钟倒计时", "宋体", 16, Color.BLACK, Color.YELLOW, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
//        ledsender.AddCounter(0, 16, 128, 16, 0, 0, 0, 0, 2, 0, 3, 0, "宋体", 16, Color.BLACK, Color.YELLOW, 0);

        send_data(ledhost, ledport);
    }

    public static void demo_play_chapterschedule() {
        //String[] formats={"#y", "年", "#m", "月", "#d", "日", " ", "#h", ":", "#n", ":", "#s"};
        //String[] formats={"#h", ":", "#n", ":", "#s"};

        //生成节目数据
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);

        ledsender.AddChapterEx(1, 1000, 1, 1, 0x7F, 2016, 8, 17, 0, 0, 0, 2016, 12, 27, 23, 59, 59);
        ledsender.AddRegion(0, 0, 64, 64);

        //点阵文字（自动换行）
        ledsender.AddLeaf(1, 2000);
        ledsender.AddText(0, 0, 64, 32, "欢迎光临abc", "宋体", 14, Color.BLACK, Color.RED, 0, 2, true, 3, 0, 2, 0, 0, 0, 0);

        ledsender.AddChapterEx(1, 1000, 1, 1, 0x7F, 2016, 4, 28, 10, 10, 0, 2016, 12, 27, 23, 15, 0);
        ledsender.AddRegion(0, 0, 64, 64);

        //点阵文字（单行，支持对齐方式）
        ledsender.AddLeaf(1, 2000);
        ledsender.AddTextEx(0, 0, 64, 32, "谢谢惠顾abc", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 14, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);

        send_data(ledhost, ledport);
    }

    public static void demo_play_picture() {
        //生成节目数据
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(0, 0, 64, 64);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddPicture(0, 0, 64, 64, "c:\\demo.bmp", 1, 0, 2, 0, 0, 0, 0);
        send_data(ledhost, ledport);
    }

    public static void demo_play_voice() {
        //生成节目数据
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(0, 0, 64, 64);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddTextEx(0, 0, 64, 32, "欢迎光临", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 16, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddVoice("欢迎光临", 5000, 10, 0, 5, 5, 1);
        send_data(ledhost, ledport);
    }

    public static void demo_datetime_play() {
        String[] formats_datetime = {"#y", "年", "#m", "月", "#d", "日", " ", "#h", ":", "#n", ":", "#s"};
        String[] formats_date = {"#y", "年", "#m", "月", "#d", "日"};
        String[] formats_time = {"#h", ":", "#n", ":", "#s"};
        String[] formats_week = {"星期", "#w"};

        //生成节目数据
        //ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
        ledsender.AddChapter(1, 10000);
        ledsender.AddRegion(0, 0, 128, 64);

        ledsender.AddLeaf(1, 2000);
        ledsender.AddDateTime(0, 0, 128, 32, formats_datetime, 12, "宋体", 14, Color.BLACK, Color.YELLOW, 0);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddDateTime(0, 0, 128, 32, formats_date, 6, "宋体", 14, Color.BLACK, Color.RED, 0);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddDateTime(0, 0, 128, 32, formats_time, 5, "宋体", 14, Color.BLACK, Color.GREEN, 0);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddDateTime(0, 0, 128, 32, formats_week, 2, "宋体", 14, Color.BLACK, Color.RED, 0);

        send_data(ledhost, ledport);
    }

    public static void demo_counter_play() {
        //生成节目数据
        //ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
        ledsender.AddChapter(1, 10000);
        ledsender.AddRegion(0, 0, 128, 64);

        ledsender.AddLeaf(1, 2000);
        ledsender.AddCounter(0, 0, 128, 16, 2020, 5, 20, 1, 20, 0, 0, 9, "宋体", 16, Color.BLACK, Color.YELLOW, 0);

        send_data(ledhost, ledport);
    }

    public static void demo_schedule_child_play() {
        //生成节目数据
        //String[] formats_datetime={"#y", "年", "#m", "月", "#d", "日", " ", "#h", ":", "#n", ":", "#s"};

        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 10000);
        ledsender.AddRegion(0, 0, 64, 64);

        ledsender.AddLeaf(1, 2000);
        ledsender.AddWindows(0, 0, 64, 32);
        //ledsender.AddChildText("abc", "宋体", 14, Color.BLACK, Color.RED, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddChildScheduleText("欢迎光临", "宋体", 14, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 2000, 2, 0x0, 2018, 2, 5, 7, 10, 0, 2018, 2, 6, 23, 15, 0);
        ledsender.AddChildScheduleText("谢谢惠顾", "宋体", 14, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 2000, 2, 0x0, 2018, 2, 5, 7, 10, 0, 2018, 2, 6, 23, 15, 0);
        ledsender.AddChildScheduleText("同一个世界", "宋体", 14, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 2000, 2, 0x0, 2018, 2, 8, 7, 10, 0, 2018, 2, 9, 23, 15, 0);
        ledsender.AddChildScheduleText("同一个梦想", "宋体", 14, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 2000, 2, 0x0, 2018, 2, 8, 7, 10, 0, 2018, 2, 9, 23, 15, 0);

        send_data(ledhost, ledport);
    }

    public static void demo_fontset_import(MyUdpSocket udp, String ip, int num, String text) {
        byte[] packet = new byte[1024];
        byte[] s;
        int i, size;
        int c;

        size = 0;
        try {
            s = text.getBytes("GB2312");
            size = 0;
            packet[size++] = 0x55;
            packet[size++] = 0x25;
            packet[size++] = 0x3E;
            packet[size++] = (byte) (((num / 100) % 10) + 0x30);
            packet[size++] = (byte) (((num / 10) % 10) + 0x30);
            packet[size++] = (byte) ((num % 10) + 0x30);
            for (i = 0; i < s.length; i++) {
                if (s[i] >= 0) c = s[i];
                else c = (256 + s[i]);
                switch (c >> 4) {
                    case 0:
                        packet[size++] = 0x30;
                        break;
                    case 1:
                        packet[size++] = 0x31;
                        break;
                    case 2:
                        packet[size++] = 0x32;
                        break;
                    case 3:
                        packet[size++] = 0x33;
                        break;
                    case 4:
                        packet[size++] = 0x34;
                        break;
                    case 5:
                        packet[size++] = 0x35;
                        break;
                    case 6:
                        packet[size++] = 0x36;
                        break;
                    case 7:
                        packet[size++] = 0x37;
                        break;
                    case 8:
                        packet[size++] = 0x38;
                        break;
                    case 9:
                        packet[size++] = 0x39;
                        break;
                    case 10:
                        packet[size++] = 0x61;
                        break;
                    case 11:
                        packet[size++] = 0x62;
                        break;
                    case 12:
                        packet[size++] = 0x63;
                        break;
                    case 13:
                        packet[size++] = 0x64;
                        break;
                    case 14:
                        packet[size++] = 0x65;
                        break;
                    case 15:
                        packet[size++] = 0x66;
                        break;
                    default:
                        packet[size++] = 0x30;
                        break;
                }
                switch (c & 0xF) {
                    case 0:
                        packet[size++] = 0x30;
                        break;
                    case 1:
                        packet[size++] = 0x31;
                        break;
                    case 2:
                        packet[size++] = 0x32;
                        break;
                    case 3:
                        packet[size++] = 0x33;
                        break;
                    case 4:
                        packet[size++] = 0x34;
                        break;
                    case 5:
                        packet[size++] = 0x35;
                        break;
                    case 6:
                        packet[size++] = 0x36;
                        break;
                    case 7:
                        packet[size++] = 0x37;
                        break;
                    case 8:
                        packet[size++] = 0x38;
                        break;
                    case 9:
                        packet[size++] = 0x39;
                        break;
                    case 10:
                        packet[size++] = 0x61;
                        break;
                    case 11:
                        packet[size++] = 0x62;
                        break;
                    case 12:
                        packet[size++] = 0x63;
                        break;
                    case 13:
                        packet[size++] = 0x64;
                        break;
                    case 14:
                        packet[size++] = 0x65;
                        break;
                    case 15:
                        packet[size++] = 0x66;
                        break;
                    default:
                        packet[size++] = 0x30;
                        break;
                }
            }
            packet[size++] = 0x23;
            packet[size++] = (byte) 0xAA;
        } catch (Exception e) {
        }

        if (size > 0) {
            try {
                udp = new MyUdpSocket(8868);
                udp.send(ip, 6666, packet, size);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    //只更新一个节目
    public static void demo_play_sub_chapter() {
        ledsender.MakeChapter(LEDSender2010.ROOT_PLAY_CHAPTER, LEDSender2010.COLOR_TYPE_DOUBLE, 1, 0, 1, 1000);
        ledsender.AddRegion(0, 0, 64, 64);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddTextEx(0, 0, 64, 32, "abc", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 14, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        send_data(ledhost, ledport);
    }

    public static void demo_play_sub_chapter_ex() {
        ledsender.MakeChapterEx(LEDSender2010.ROOT_PLAY_CHAPTER, LEDSender2010.COLOR_TYPE_DOUBLE, 1, 0, 1, 1000, 1, 1, 0x7F, 2016, 8, 17, 0, 0, 0, 2016, 12, 27, 23, 59, 59);
        ledsender.AddRegion(0, 0, 64, 64);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddTextEx(0, 0, 64, 32, "abc", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 14, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        send_data(ledhost, ledport);
    }

    //只更新一个分区
    public static void demo_play_sub_region() {
        ledsender.MakeRegion(LEDSender2010.ROOT_PLAY_REGION, LEDSender2010.COLOR_TYPE_DOUBLE, 1, 0, 1, 0, 16, 128, 16);
        ledsender.AddLeaf(1, 2000);
        ledsender.AddTextEx(0, 0, 64, 32, "abc", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 14, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        send_data(ledhost, ledport);
    }

    //只更新一个页面
    public static void demo_play_sub_leaf() {
        String[] formats_datetime = {"#h", ":", "#n", ":", "#s"};
        ledsender.MakeLeaf(LEDSender2010.ROOT_PLAY_LEAF, LEDSender2010.COLOR_TYPE_DOUBLE, 1, 0, 0, 1, 1, 1000);
        ledsender.AddTextEx(0, 0, 64, 16, "abc", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 14, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddDateTime(0, 16, 64, 16, formats_datetime, 12, "宋体", 14, Color.BLACK, Color.YELLOW, 0);
        send_data(ledhost, ledport);
    }

    //只更新一个对象
    public static void demo_play_sub_object() {
        ledsender.MakeObject(LEDSender2010.ROOT_PLAY_OBJECT, LEDSender2010.COLOR_TYPE_DOUBLE, 1, 0, 0, 0, 1);
        //此处只能添加一个对象
        ledsender.AddTextEx(0, 0, 64, 32, "abc", LEDSender2010.ALIGN_CENTER, LEDSender2010.ALIGN_TOP, "宋体", 14, Color.BLACK, Color.RED, 0, 1, 0, 2, 0, 0, 0, 0);
        send_data(ledhost, ledport);
    }

    public static void demo_listen_report() {
        int x, k;
        int size;
        byte[] packet = new byte[1280];
        log.info("正在监听心跳包...");
        for (x = 0; x < 1000; x++) {
            size = udp_receive(packet);
            if (size > 0) {
                k = ledsender.parse_heart(packet, size, my_udp.get_client_ip(), my_udp.get_client_port());
                if (k >= 0) {
                    log.info("收到心跳包");
                    log.info("Name=" + ledsender.get_report_name(k));
                    log.info("IP=" + ledsender.get_report_ip(k));
                    log.info("Port=" + ledsender.get_report_port(k));
                }
            }
        }

        //发送名称为LED1的显示屏
        log.info("发送名称为LED1的显示屏...");
        x = ledsender.find_report_name("LED1");
        if (x >= 0) {

            //以当前系统时间校时
            size = ledsender.pkg_adjust_time(packet, (byte) addr);
            log.info("校正时间...");
            do_command(packet, size, ledsender.get_report_ip(x), ledsender.get_report_port(x));

            ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
            ledsender.AddChapter(1, 1000);
            ledsender.AddRegion(0, 0, 256, 64);
            ledsender.AddLeaf(1, 2000);
            ledsender.AddTextEx(0, 0, 128, 32, "欢迎光临谢谢惠顾", LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 24, Color.BLACK, new Color(255, 255, 255), 0, 1, 0, 2, 0, 0, 0, 0);
            send_data(ledsender.get_report_ip(x), ledsender.get_report_port(x));
        } else {
            log.info("监听列表中没有该显示屏");
        }
    }

    public static void demo_wan() {
        WanService wan_service;
        long tick = 0;
        String text;
        int i = 1;

        //创建广域网服务对象
        wan_service = new WanService(8888);

        //执行命令例程
        i = 0;
        while (true) {

            //执行广域网服务
            wan_service.execute();

            //////////////////////////////////////////////////////////////////////////////////////////
            //  执行控制命令，为命令模式
            //////////////////////////////////////////////////////////////////////////////////////////
            if (System.currentTimeMillis() > tick) {
                //i==2表示执行完开启电源和校正时间命令
                if (i == 2) break;
                tick = System.currentTimeMillis() + 5000;
                switch (i) {
                    case 0:
                        log.info("执行开启电源命令");
                        switch (wan_service.AddTask("led", WanService.TASK_COMMAND, 0, addr, ledsender.packet_stream, ledsender.pkg_set_power(addr, 1))) {
                            case WanService.R_BUSY:
                                log.info("设备忙，通讯中...");
                                break;
                            case WanService.R_OFFLINE:
                                log.info("设备不在线");
                                break;
                            case WanService.R_EXECUTE:
                                log.info("执行命令任务已提交");
                                i++;
                                break;
                        }
                        break;
                    case 1:
                        log.info("执行校正时间命令");
                        switch (wan_service.AddTask("led", WanService.TASK_COMMAND, 0, addr, ledsender.packet_stream, ledsender.pkg_adjust_time(addr))) {
                            case WanService.R_BUSY:
                                log.info("设备忙，通讯中...");
                                break;
                            case WanService.R_OFFLINE:
                                log.info("设备不在线");
                                break;
                            case WanService.R_EXECUTE:
                                log.info("执行命令任务已提交");
                                i++;
                                break;
                        }
                        break;
                }
            }
        }

        //执行数据发送例程
        i = 1;
        while (true) {

            //执行广域网服务
            wan_service.execute();

            if (System.currentTimeMillis() > tick) {
                if (i == 100) break;
                tick = System.currentTimeMillis() + 5000;
                log.info("第" + i + "次发送");

                //////////////////////////////////////////////////////////////////////////////////////////
                //  发送显示节目
                //////////////////////////////////////////////////////////////////////////////////////////
                ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
                ledsender.AddChapter(1, 1000);
                ledsender.AddRegion(0, 0, 256, 64);
                ledsender.AddLeaf(1, 2000);
                text = "测试" + i;
                ledsender.AddTextEx(0, 0, 128, 32, text, LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 24, Color.BLACK, new Color(255, 255, 255), 0, 27, 0, 2, 0, 0, 0, 0);
                switch (wan_service.AddTask("led", WanService.TASK_DATA, 0, 0, ledsender.data_stream, ledsender.GetDataSize())) {
                    case WanService.R_BUSY:
                        log.info("设备忙，通讯中...");
                        break;
                    case WanService.R_OFFLINE:
                        log.info("设备不在线");
                        break;
                    case WanService.R_EXECUTE:
                        log.info("发送任务已提交");
                        break;
                }

                i++;
            }
            mysleep(1);
        }
    }

    public static void demo_wan_thread() {
        WanServiceThread wan_service;
        long tick = 0;
        int addr = 0;
        String text;
        int i = 1;

        //启动广域网服务线程
        wan_service = new WanServiceThread(8888);
        wan_service.start();

        //执行命令例程
        i = 0;
        while (true) {
            //////////////////////////////////////////////////////////////////////////////////////////
            //  执行控制命令，为命令模式
            //////////////////////////////////////////////////////////////////////////////////////////
            if (System.currentTimeMillis() > tick) {
                //i==2表示执行完开启电源和校正时间命令
                if (i == 2) break;
                tick = System.currentTimeMillis() + 5000;
                switch (i) {
                    case 0:
                        log.info("执行开启电源命令");
                        switch (wan_service.AddTask("led", WanService.TASK_COMMAND, 0, addr, ledsender.packet_stream, ledsender.pkg_set_power(addr, 1))) {
                            case WanService.R_BUSY:
                                log.info("设备忙，通讯中...");
                                break;
                            case WanService.R_OFFLINE:
                                log.info("设备不在线");
                                break;
                            case WanService.R_EXECUTE:
                                log.info("执行命令任务已提交");
                                i++;
                                break;
                        }
                        break;
                    case 1:
                        log.info("执行校正时间命令");
                        switch (wan_service.AddTask("led", WanService.TASK_COMMAND, 0, addr, ledsender.packet_stream, ledsender.pkg_adjust_time(addr))) {
                            case WanService.R_BUSY:
                                log.info("设备忙，通讯中...");
                                break;
                            case WanService.R_OFFLINE:
                                log.info("设备不在线");
                                break;
                            case WanService.R_EXECUTE:
                                log.info("执行命令任务已提交");
                                i++;
                                break;
                        }
                        break;
                }
            }
        }

        //执行数据发送例程
        i = 1;
        while (true) {

            if (System.currentTimeMillis() > tick) {
                if (i == 100) break;
                tick = System.currentTimeMillis() + 5000;
                log.info("第" + i + "次发送");

                //////////////////////////////////////////////////////////////////////////////////////////
                //  发送显示节目，为数据模式
                //////////////////////////////////////////////////////////////////////////////////////////
    	        /*
    	    	ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_THREE);
    	        ledsender.AddChapter(1, 1000);
    	        ledsender.AddRegion(0, 0, 256, 64);
    	        ledsender.AddLeaf(1, 2000);
    	        text = "测试" + i;
    	        ledsender.AddTextEx(0, 0, 128, 32, text, LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 24, Color.BLACK, new Color(255,255,255), 0, 27, 0, 2, 0, 0, 0, 0);
    	        */

                //////////////////////////////////////////////////////////////////////////////////////////
                //  发送显示节目，局部刷新，更新一个分区，为数据模式
                //////////////////////////////////////////////////////////////////////////////////////////
    	        /*
    	    	ledsender.MakeRegion(LEDSender2010.ROOT_PLAY_REGION, LEDSender2010.COLOR_TYPE_THREE, 1, 0, 0, 0, 0, 128, 16);
    	        ledsender.AddLeaf(1, 2000);
    	        text = "测试" + i;
    	        ledsender.AddTextEx(0, 0, 128, 32, text, LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 16, Color.BLACK, new Color(255,255,255), 0, 27, 0, 2, 0, 0, 0, 0);
    	        */

                //////////////////////////////////////////////////////////////////////////////////////////
                //  发送显示节目，局部刷新，更新一个对象，为数据模式
                //////////////////////////////////////////////////////////////////////////////////////////
                ledsender.MakeObject(LEDSender2010.ROOT_PLAY_OBJECT, LEDSender2010.COLOR_TYPE_THREE, 1, 0, 1, 0, 1);
                text = "测试" + i;
                ledsender.AddTextEx(64, 0, 64, 16, text, LEDSender2010.ALIGN_LEFT, LEDSender2010.ALIGN_TOP, "宋体", 16, Color.BLACK, new Color(255, 255, 255), 0, 27, 0, 2, 0, 0, 0, 0);

                switch (wan_service.AddTask("led", WanService.TASK_DATA, 0, addr, ledsender.data_stream, ledsender.GetDataSize())) {
                    case WanService.R_BUSY:
                        log.info("设备忙，通讯中...");
                        break;
                    case WanService.R_OFFLINE:
                        log.info("设备不在线");
                        break;
                    case WanService.R_EXECUTE:
                        log.info("发送节目任务已提交");
                        break;
                }

                i++;
            }
            mysleep(1);
        }

        wan_service.Terminate();
    }

    public static void main(String[] args) throws InterruptedException {
        call("A030");
    }

    public static void call(String msg) {
        //生成节目数据
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(0, 0, 384, 32);
        ledsender.AddLeaf(0, 20000);
        ledsender.AddText(0, 18, 64, 16, "请" + msg + "号办理", "宋体", 12, Color.BLACK, Color.RED, 0, 0, false, 2, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(66, 18, 64, 16, "热情服务您", "宋体", 12, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(130, 18, 64, 16, "热情服务您", "宋体", 12, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(194, 18, 64, 16, "请C013号办理", "宋体", 12, Color.BLACK, Color.RED, 0, 0, false, 2, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(258, 18, 64, 16, "热情服务您", "宋体", 12, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(322, 18, 64, 16, "热情服务您", "宋体", 12, Color.BLACK, Color.RED, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(2, 2, 64, 16, "①业务咨询", "宋体", 12, Color.BLACK, Color.GREEN, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(66, 2, 64, 16, "②社保办理", "宋体", 12, Color.BLACK, Color.GREEN, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(130, 2, 64, 16, "③社保办理", "宋体", 12, Color.BLACK, Color.GREEN, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(194, 2, 64, 16, "④社保办理", "宋体", 12, Color.BLACK, Color.GREEN, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(258, 2, 64, 16, "⑤综合服务", "宋体", 12, Color.BLACK, Color.GREEN, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);
        ledsender.AddText(322, 2, 64, 16, "⑥综合服务", "宋体", 12, Color.BLACK, Color.GREEN, 0, 0, false, 1, 0, 2, 0, 0, 0, 0);

        send_data(ledhost, ledport);
    }

    public static void call(JSONArray array) {
        //生成节目数据
        ledsender.MakeRoot(LEDSender2010.ROOT_PLAY, LEDSender2010.COLOR_TYPE_DOUBLE);
        ledsender.AddChapter(1, 1000);
        ledsender.AddRegion(0, 0, 384, 32);
        ledsender.AddLeaf(0, 20000);
        for (Object o : array) {
            JSONObject text = (JSONObject) o;
            Integer left = text.getInteger("left");
            Integer top = text.getInteger("top");
            Integer width = text.getInteger("width");
            Integer height = text.getInteger("height");
            String t = text.getString("text");
            String font = text.getString("fontName");
            Integer inmethod = text.getInteger("inmethod");
            String fontBackColor = text.getString("fontBackColor");
            String fontColor = text.getString("fontColor");
            ledsender.AddText(left, top, width, height, t, font, 12, getColor(fontBackColor), getColor(fontColor),
                    0, 0, false, inmethod, 0, 2, 0, 0, 0, 0);
        }
        send_data(ledhost, ledport);
    }

    private static Color getColor(String fontColor) {
        String[] splitFontColor = fontColor.split(",");
        int s0 = Integer.parseInt(splitFontColor[0]);
        int s1 = Integer.parseInt(splitFontColor[1]);
        int s2 = Integer.parseInt(splitFontColor[2]);
        return new Color(s0, s1, s2);
    }

}
